package com.bobby.shop.service.impl;

import com.bobby.shop.mapper.ArticleMapper;
import com.bobby.shop.mapper.ShopCarMapper;
import com.bobby.shop.pojo.Article;
import com.bobby.shop.pojo.ShopCar;
import com.bobby.shop.pojo.ShopCarExample;
import com.bobby.shop.service.ShopCarService;
import com.bobby.shop.vo.ShopCarVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Date 2020/12/22 20:05
 * @Created wenjunpei
 */
@Service
@Transactional
public class ShopCarServiceImpl implements ShopCarService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ShopCarMapper shopCarMapper;

    @Override
    public ShopCar findByArticleIdAndUserId(String articleId,Integer userId) {
        ShopCarExample shopCarExample = new ShopCarExample();
        ShopCarExample.Criteria criteria = shopCarExample.createCriteria();
        criteria.andArticleidEqualTo(Integer.parseInt(articleId));
        criteria.andUseridEqualTo(userId);
        List<ShopCar> shopCars = shopCarMapper.selectByExample(shopCarExample);
        if(shopCars.size() >= 1) {
            return shopCars.get(0);
        }else {
            return null;
        }
    }

    @Override
    public void save(ShopCar shopCar) {
        if(shopCar.getId() == null) {
            shopCarMapper.insert(shopCar);
        }else {
            shopCarMapper.updateByPrimaryKey(shopCar);
        }
    }

    @Override
    public List<ShopCarVO> findByUserId(Integer userId) {
        ShopCarExample shopCarExample = new ShopCarExample();
        ShopCarExample.Criteria criteria = shopCarExample.createCriteria();
        criteria.andUseridEqualTo(userId);
        List<ShopCar> shopCarList = shopCarMapper.selectByExample(shopCarExample);
        List<ShopCarVO> shopCarVOList = new ArrayList<>();
        for (ShopCar shopCar : shopCarList) {
            Article article = articleMapper.selectByPrimaryKey(shopCar.getArticleid());
            ShopCarVO shopCarVO = new ShopCarVO();
            BeanUtils.copyProperties(article,shopCarVO);
            shopCarVO.setBuyNum(shopCar.getBuynum());
            shopCarVO.setArticleId(article.getId());
            shopCarVO.setShopCarId(shopCar.getId());
            shopCarVO.setStorage(article.getStorage());
            shopCarVOList.add(shopCarVO);
        }
        return shopCarVOList;
    }

    @Override
    public ShopCar findByShopCarId(String shopCarId) {
        return shopCarMapper.selectByPrimaryKey(Integer.parseInt(shopCarId));
    }

    @Override
    public void deleteByShopCarId(String shopCarId) {
        shopCarMapper.deleteByPrimaryKey(Integer.parseInt(shopCarId));
    }

    @Override
    public void updateShopCar(ShopCar shopCar) {
        shopCarMapper.updateByPrimaryKey(shopCar);
    }


}
